package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Cc
 * @version 1.0
 */
@RequestMapping("/admin/role")
@RestController
@CrossOrigin
public class AdminRoleController {

    @Autowired
    private DtsRoleService roleService;

    /**
     * 查询所有角色
     * - 接口地址 : http://localhost:8083/admin/role/options
     */
    @GetMapping("/options")
    public Object options() {
        List<DtsRole> roleList = roleService.findRoleList();

        //Vo :
        List<CatVo> catVoList = roleList.stream().map(dtsRole -> {
            CatVo vo = new CatVo();
            vo.setValue(dtsRole.getId());
            vo.setLabel(dtsRole.getName());
            return vo;
        }).collect(Collectors.toList());
        return ResponseUtil.ok(catVoList);
    }


    /**
     * 角色列表
     *
     * @param
     * @return Response
     * 接口地址 : http://localhost:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {
        IPage<DtsRole> iPage = roleService.querySelective(name, page, limit, sort, order);
        long total = iPage.getTotal();
        List<DtsRole> roleList = iPage.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", roleList);

        return ResponseUtil.ok(data);
    }
}
